package models

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/model"
	"gorm.io/datatypes"
	cDto "ssdevice/common/dto"
	"ssdevice/internal/device/models/query"
)

type SysBatchTask struct {
	Id         string         `json:"id" gorm:"primaryKey;type:varchar(255);comment:id"`
	Name       string         `json:"name" gorm:"type:varchar(255);comment:任务名称"`
	Status     int            `json:"status" gorm:"type:varchar(255);comment:任务状态"`
	UpdateType string         `json:"updateType" gorm:"type:varchar(255);comment:更新类型：all part"`
	PackageId  string         `json:"packageId" gorm:"type:varchar(255);comment:安装包ID"`
	ProductId  string         `json:"productId" gorm:"type:varchar(255);comment:产品ID"`
	ConfigData datatypes.JSON `json:"configData" gorm:"type:jsonb;comment:配置数据，用于批量配置"`
	TaskType   string         `json:"taskType" gorm:"type:varchar(255);comment:任务类型OTA、restart、config"`
	Filters    datatypes.JSON `json:"filters" gorm:"type:jsonb;comment:过滤条件"`

	CreateTime *cDto.LocalTime `json:"createTime" gorm:"type:timestamptz(6);comment:创建时间"`
	UpdateTime *cDto.LocalTime `json:"updateTime" gorm:"type:timestamptz(6);comment:更新时间"`
	CreateBy   string          `json:"createBy" gorm:"type:varchar(255);comment:创建人"`
	model.Model
}

func (SysBatchTask) TableName() string {
	return "sys_batch_task"
}

func (e *SysBatchTask) Generate() interface{} {
	o := *e
	return &o
}

func (e *SysBatchTask) GetId() interface{} {
	return e.Id
}

func (e *SysBatchTask) FindTaskList(req *query.SysBatchTaskQuery) (err error, result interface{}) {
	list := make([]SysBatchTask, 0)
	var count int64
	if req.PageIndex > 0 && req.PageSize > 0 {
		err = e.FindPage(&list, &count,
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.OrderDest("create_time", true, req.OrderBy),
			cDto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		)
		result = &cDto.Page{
			Records:   list,
			Total:     count,
			PageSize:  req.GetPageSize(),
			PageIndex: req.GetPageIndex(),
			Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
			Other:     nil,
		}
	} else {
		err = e.FindList(&list,
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.OrderDest("create_time", true, req.OrderBy),
		)
		result = list
	}
	return
}

type SysDevVersion struct {
	Sn         string `json:"sn" gorm:"type:varchar(255);comment:sn"`
	DevVersion string `json:"devVersion" gorm:"type:varchar(255);comment:版本号"`
	model.Model
}

func (SysDevVersion) TableName() string {
	return "sys_dev_version"
}

func (e *SysDevVersion) Generate() interface{} {
	o := *e
	return &o
}
